/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.l2emu.gameserver.handler.admincommandhandlers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.logging.Logger;

import com.l2emu.L2DatabaseFactory;
import com.l2emu.gameserver.Announcements;
import com.l2emu.gameserver.datatables.GmListTable;
import com.l2emu.gameserver.handler.IAdminCommandHandler;
import com.l2emu.gameserver.model.L2Object;
import com.l2emu.gameserver.model.actor.instance.L2PcInstance;

/**
 *
 * @author Rayan
 * @project L2Emu Project
 * @Since 2528
 *
 */
public class AdminDonator implements IAdminCommandHandler
{
	private static String[]		ADMIN_COMMANDS	=
												{ "admin_setdonator" };
	static Logger _log = Logger.getLogger(AdminDonator.class.getName());

	public boolean useAdminCommand(String command, L2PcInstance activeChar)
	{
		if (activeChar == null)
			return false;

		if (command.startsWith("admin_setdonator"))
		{
			L2Object target = activeChar.getTarget();

			if (target instanceof L2PcInstance)
			{
				L2PcInstance targetPlayer = (L2PcInstance) target;
				boolean newDonator = !targetPlayer.isDonator();

				if (newDonator)
				{
					targetPlayer.setDonator(true);
					targetPlayer.updateNameTitleColor();
					updateDatabase(activeChar, true);
					sendMessages(true, targetPlayer, activeChar, true, true);
				}
				else
				{
					targetPlayer.setDonator(false);
					targetPlayer.updateNameTitleColor();
					updateDatabase(activeChar, false);
					sendMessages(false, targetPlayer, activeChar, true, true);
				}
			}
			else
			{
				activeChar.sendMessage("impossible to set a non Player Target as Donator.");
				_log.info("GM: " + activeChar.getName() + " is trying to set a non Player Target as Donator.");
				return false;
			}
		}
		return true;
	}

	private void sendMessages(boolean forNewDonator, L2PcInstance player, L2PcInstance gm, boolean announce, boolean notifyGmList)
	{
		if (forNewDonator)
		{
			player.sendMessage(gm.getName() + " has granted Donator Status for you!");
			gm.sendMessage("you've granted Donator Status for " + player.getName());

			if (announce)
				Announcements.getInstance().announceToAll(player.getName() + " has  Received Donator Status!");

			if (notifyGmList)
				GmListTable.broadcastMessageToGMs("Warn: " + gm.getName() + " has Set " + player.getName() + " as Donator !");
		}
		else
		{
			player.sendMessage(gm.getName() + " has revoked Donator Status for you!");
			gm.sendMessage("you've revoked Donator Status for " + player.getName());

			if (announce)
				Announcements.getInstance().announceToAll(player.getName() + " has  Lost Donator Status!");

			if (notifyGmList)
				GmListTable.broadcastMessageToGMs(gm.getName() + " has Removed Donator Status of Player" + player.getName());
		}
	}

	/**
	 *
	 * @param activeChar
	 * @param newDonator
	 */
	private void updateDatabase(L2PcInstance player, boolean newDonator)
	{
		Connection con = null;
		try
		{
			// prevents any NPE.
			// ----------------
			if (player == null)
				return;

			// Database Connection
			//--------------------------------
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement stmt = con.prepareStatement(newDonator ? INSERT_DATA : DEL_DATA);

			// if it is a new donator insert proper data
			// --------------------------------------------
			if (newDonator)
			{
				//charId, char_name, hero, noble, donator
				stmt.setInt(1, player.getObjectId());
				stmt.setString(2, player.getName());
				stmt.setInt(3, player.isHero() ? 1 : 0);
				stmt.setInt(4, player.isNoble() ? 1 : 0);
				stmt.setInt(5, 1);// sets new player as donator
				stmt.execute();
				stmt.close();
			}
			else
			// deletes from database
			{
				stmt.setInt(1, player.getObjectId());
				stmt.execute();
				stmt.close();
			}
		}
		catch (Exception e)
		{
			_log.warning("Error: could not update database");
		}
		finally
		{
			try
			{
				con.close();
			}
			catch (Exception e)
			{
				_log.warning("Error: could not update database");
			}
		}
	}

	// Updates That Will be Executed by MySQL
	// ----------------------------------------
	String	INSERT_DATA	= "REPLACE INTO characters_custom_data (charId, char_name, hero, noble, donator) VALUES (?,?,?,?,?)";
	String	DEL_DATA	= "UPDATE characters_custom_data SET donator = 0 WHERE charId=?";

	/**
	 * @return
	 */
	public String[] getAdminCommandList()
	{
		return ADMIN_COMMANDS;
	}
}